import lombok.Getter;
import net.sf.cglib.proxy.Dispatcher;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.LazyLoader;

@Getter
class LazyTeacher {
  private String name;
  private int age;
  public LazyTeacher(String name, int age){
    this.name = name;
    this.age = age;
  }
  public LazyTeacher() {
  }
}

class ConcreteClassLazyLoader implements Dispatcher {
  private String name;
  private int age;
  @Override
  public Object loadObject() throws Exception{
    System.out.println("lazy loader");
    return new LazyTeacher(name, age);
  }
  public ConcreteClassLazyLoader(String name, int age){
    this.name = name;
    this.age = age;
  }
}
@Getter
class Student {
  private String name;
  private int age;
  private LazyTeacher teacher;
  public Student(String name, int age) {
    this.name = name;
    this.age = age;
    this.teacher = createLazyTeacher("sb", 250);
  }
  private LazyTeacher createLazyTeacher(String name, int age) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(LazyTeacher.class);
    LazyTeacher teacher = (LazyTeacher) enhancer.create(LazyTeacher.class, new ConcreteClassLazyLoader(name, age));
    return teacher;
  }
  public String toString(){
    return "name: " + name + ",age: " + age;
  }
  public String sayTeacher() {
    return "name: " + name + ",age: " + age + ",teacher name: " + teacher.getName() + ",teacher age: " + teacher.getAge();
  }
}



public class Lazy {
  public static void main(String[] args) {
    Student student = new Student("mihu", 18);
    System.out.println(student+"");

    LazyTeacher teacher = student.getTeacher();
    teacher.getAge();
    LazyTeacher teacher1 = student.getTeacher();
    teacher1.getAge();

    System.out.println(teacher == teacher1);
  }
}
